package com.xbts.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.core.domain.Setting;
import com.xbts.common.enums.SettingEnum;
import com.xbts.system.domain.dto.AccountItemListDTO;
import com.xbts.system.domain.dto.AccountSetting;
import com.xbts.system.mapper.SettingMapper;
import com.xbts.system.service.SettingService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 配置业务层实现
 *
 * @author Chopper
 * @since 2020/11/17 3:52 下午
 */
@Service
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements SettingService {

    @Override
    public Setting get(String key) {
        return this.getById(key);
    }

    @Override
    public boolean saveUpdate(Setting setting) {
        return this.saveOrUpdate(setting);
    }

    @Override
    public AccountSetting getAccountSetting(SettingEnum key) {
        Setting setting = this.getById(SettingEnum.ACCOUNT_SETTING);
        AccountItemListDTO accountItemListDTO = JSON.parseObject(setting.getSettingValue(), AccountItemListDTO.class);
        List<AccountSetting> accountSettingList = accountItemListDTO.getAccountItemList();
        for (AccountSetting accountSetting : accountSettingList) {
            if (accountSetting.getCloudServerType().equals(key)) {
                return accountSetting;
            }
        }
        return null;
    }

}